<?php
// +----------------------------------------------------------------------
// | 后台用户管理
// +----------------------------------------------------------------------
namespace app\admin\model;
use think\Model;
use base\other\Strings;

class SystemUser extends Model
{
	protected $hidden = ["user_pwd"];
    protected $name = 'system_user';  //表名
	protected $pk = 'user_id';  //主键		
	
	
	//类型自动转换
	protected $type = [
        'land_time'  =>   'datetime',   //  '2015/5/1' ->  '2015-05-01 00:00:00'
        'create_time'  =>'datetime' //  '2015/5/1' ->  '2015-05-01 00:00:00'
    ];
	
    //tp6模型的自动完成功能已经取消，请使用模型事件代替。
    public static function onBeforeInsert($data) {
    	$data['user_pwd'] = $data['user_pwd'] ?:  md5('wei'.md5( request()->param("user_pwd") ?: 1 ));
    	$data['user_guid'] = $data['user_guid'] ?:  Strings::uuid();
    	$data['register_ip'] = $data['register_ip'] ?:  request()->ip();
    	//$data['land_ip'] = $data['land_ip'] ?:  request()->ip();
    	//$data['land_time'] = $data['land_time'] ?: date("Y-m-d H:i:s");
    }
    
	
	/**
     * 修改密码
     * @param type $user_id
     * @param type $new_pwd
     * @return type
     */
	public function updatePwd($user_id,$new_pwd)
    {
		return $this ->where("user_id",$user_id)->update(['user_pwd' =>$new_pwd]);
    }
	
	/**
     * 新增用户
     * @param type $data
     * @return type
     */
	public function addUser($data)
    {
		return self::save($data);
    }
	
	/**
     * 启用或禁用
     * @param array $data 
     * @return type
     */
	public function updates($data)
    {
		return self::saveAll($data);
    }
	
	/**
     * 根据组织查找用户
     * @param type $orgIds
     * @return type
     */
	public function selectOrgUser($orgIds)
    {
		return $this->where("org_id",'in',$orgIds)->select();
    }
	
}
